// pages/my/privateInfo/privateInfo.js
const LApp = getApp();
Page({


  /**
   * 页面的初始数据
   */
  data: {
    UserModel: {},
    imagepath: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    LApp._get({
      url: "nbapi/my/getuser",
      data: { userid: LApp.globalData.userInfo.UserID },
      success: function (r) {
        that.setData({
          UserModel: r,
        });
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  gobindingPhone: function () {
    wx.navigateTo({
      url: '../accountBinding/bindingPhone/bindingPhone',
    })
  },
  replace: function () {   
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      success: function (res) {
        //让头像先显示出来
        that.data.UserModel.UserImg = res.tempFilePaths;
        that.setData({
          UserModel: that.data.UserModel,
        });    
        //这里是上传头像     
        var tempFilePaths =that.data.UserModel.UserImg;
        LApp.uploadFile({
          url: 'nbapi/comm/file/upload',
          filepath: tempFilePaths[0],
          file: "files",
          success: function (r) {
            //把OSS路径返回在赋值给对象
            that.data.UserModel.UserImg = r.result;
            that.setData({
              UserModel: that.data.UserModel,
            });    
          }
        }) 
      },
    })    
  },
  setRealName:function(e){
    this.data.UserModel.RealName=e.detail.value;
    this.setData({
      UserModel: this.data.UserModel,
    });
  },
  confirm:function(){
    var that=this;
    LApp._post({
      url: "nbapi/my/edituser",
      data: this.data.UserModel,
      success: function (r) {
        LApp.globalData.userInfo.RealName = that.data.UserModel.RealName;
        LApp.globalData.userInfo.UserImg = that.data.UserModel.UserImg;
        wx.showToast({
          title: r,
          icon: 'success',
          duration: 2000
        });
        wx.reLaunch({
          url: '../my',
        })
      }
    });

  }
})